//
//  LXTypeShowViewController.m
//  cat
//
//  Created by imera on 16/3/4.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXTypeShowViewController.h"
#import "AFNetworking.h"
#import "LXVideo.h"
#import "MBProgressHUD+MJ.h"
#import "SlideBarCell.h"

@interface LXTypeShowViewController () <UITableViewDataSource, UITableViewDelegate>
/** 导航栏 */
@property (nonatomic, weak) UIView *navView;
/** 返回按钮 */
@property (nonatomic, weak) UIButton *backBtn;
/** tableview视图 */
@property (nonatomic, weak) UITableView *tableView;
/** 分类id */
@property (nonatomic, assign) int typeID;
/** 视频数组 */
@property (nonatomic, strong) NSArray *videoArr;

@end

@implementation LXTypeShowViewController

+ (instancetype)initWithTypeID:(int)typeID
{
    return [[self alloc]initWithTypeID:typeID];
}


- (instancetype)initWithTypeID:(int)typeID
{
    self.typeID = typeID;
    return [self init];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadData];
    
    [self initSubViews];
}

- (void)loadData
{
    //获取数据
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    NSString *url = [NSString stringWithFormat:@"http://cat666.com/cat666-interface/index.php/index/getCat/%d",self.typeID];
    [mgr GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id response) {
        //字典转模型
        NSArray *videos = response;
        
        NSMutableArray *videoArr = [NSMutableArray array];
        for (NSDictionary *dict in videos) {
            LXVideo *video = [[LXVideo alloc]initWithDict:dict];
            [videoArr addObject:video];
        }
        
        if (videoArr.count % 2 == 1) { // 奇数个
            [videoArr removeObjectAtIndex:videoArr.count - 1];
        }
        self.videoArr = videoArr;
        
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        [MBProgressHUD showError:@"您的网络开小差了..."];
    }];
}

#pragma mark - 初始化子控件
- (void)initSubViews
{
    /** 初始化导航栏 */
    UIView *navView = [[UIView alloc]init];
    navView.backgroundColor = MainBackgroundBlueColor;
    navView.frame = CGRectMake(0, 20, self.view.frame.size.width, 40);
    [self.view addSubview:navView];
    self.navView = navView;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    CGFloat backBtnW = 30;
    CGFloat backBtnH = 30;
    CGFloat backBtnX = LXSearchTableViewControllerMargin;
    CGFloat backBtnY = (navView.frame.size.height - backBtnH) / 2;
    backBtn.frame = CGRectMake(backBtnX, backBtnY, backBtnW, backBtnH);
    [navView addSubview:backBtn];
    self.backBtn = backBtn;
    
    UILabel *label = [[UILabel alloc]init];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"category" ofType:@"plist"];
    NSDictionary *categoryDict = [NSDictionary dictionaryWithContentsOfFile:path];
    for (NSString *key in categoryDict.allKeys) {
        if ([categoryDict[key] integerValue] == self.typeID) {
            label.text = key;
        }
    }
    label.textAlignment = NSTextAlignmentCenter;
    CGFloat labelW = 280;
    CGFloat labelH = backBtnH;
    CGFloat labelX = (navView.frame.size.width - labelW) / 2;
    CGFloat labelY = backBtnY;
    label.frame = CGRectMake(labelX, labelY, labelW, labelH);
    [navView addSubview:label];
    
    UITableView *tableView = [[UITableView alloc]init];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    CGFloat tableViewX = 0;
    CGFloat tableViewY = navView.frame.size.height + 20;
    CGFloat tableViewW = self.view.frame.size.width;
    CGFloat tableViewH = self.view.frame.size.height - navView.frame.size.height - 20;
    tableView.frame = CGRectMake(tableViewX, tableViewY, tableViewW, tableViewH);
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

#pragma mark  - 推出控制器
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];;
}

#pragma mark tableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.videoArr.count / 2;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}

- (SlideBarCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"Cell";
    
    SlideBarCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[SlideBarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.videos = @[self.videoArr[indexPath.row * 2], self.videoArr[indexPath.row * 2 + 1]];
    return cell;
}

@end
